/*
 * Copyright (C) 2012 Tomas Shestakov. <http://code.google.com/p/jmmo/>
 */

package jmmo.engine.property

import org.scalatest.matchers.ShouldMatchers
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.scalatest.{BeforeAndAfterAll, WordSpec}

/**
 * @author Tomas Shestakov
 */
class LinkedPropertySpec(linkedProp: LinkedProperty[Int]) extends WordSpec
                                                       with ShouldMatchers with MockitoSugar with BeforeAndAfterAll {

  def this() = this(LinkedProperty[Int]())

  val p = Property(9)
  linkedProp.link = p

  override val nestedSuites = List(new PropertySpec(linkedProp))

  val mockListenerInt = mock[ChangeListeners.Listener]
  when(mockListenerInt.isDefinedAt(org.mockito.Matchers.any())).thenReturn(true)

  "A LinkedProperty" when afterWord("link var") {

    "sets to the parent property on which this will depends (means property is linked)" should {


      "be readable" in {
        //linkedProp.link = p
        linkedProp.isReadable should be (true)
      }
  
      "apply method returns parent property value" in {
        linkedProp() should be (9)
      }

      "changes parent property value produce ChangePropertyEvent of depended property" in {
          linkedProp.addChangeListener(mockListenerInt)
          p() = 1
          verify(mockListenerInt).apply(ChangePropertyEvent(linkedProp, 9, 1))
//          verifyNoMoreInteractions(mockListenerInt)
      }
    }

    "sets to null (means property is not linked)" should {

      "be not readable" in {
        linkedProp.link = null

        linkedProp.isReadable should be (false)
      }

      "apply method produce UnsupportedOperationException" in {
        evaluating {
          linkedProp()
        } should produce [UnsupportedOperationException]
      }

      "changes former parent property value not produce any event of depended property" in {
        p() = 2
        verify(mockListenerInt, never()).apply(ChangePropertyEvent(linkedProp, 1, 2))
//        verifyNoMoreInteractions(mockListenerInt)
      }
    }
  }
}
